<?php

/**
 * program actions.
 *
 * @package    sf_sandbox
 * @subpackage program
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class programActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        //$this->forward('default', 'module');
    }

    public function executeCard(sfWebRequest $request) {
        $this->setLayout('popupLayout');

        $id = $this->getRequestParameter('id');
        $this->program = ProgramTable::getInstance()->find($id);
        $this->forward404Unless($this->program);

        $this->program_form = new ProgramForm($this->program);
        unset(
                $this->program_form['created_at']
                , $this->program_form['updated_at']
                , $this->program_form['created_by_user_id']
                , $this->program_form['updated_by_user_id']
                , $this->program_form['code_sector_list']
                , $this->program_form['program_type']
                , $this->program_form['global_program_type']
        );
    }

    public function executeTrainerPrograms(sfWebRequest $request) {
        // ------
        if (!$this->getUser()->hasAttribute('kts_global_program_filter')) {

            $defaults = AcademicYearTable::getInstance()->getDefaults();

            $data['year'] = $defaults['id'];
            $data['semester'] = $defaults['semseter_number'];

            $this->getUser()->setAttribute('kts_global_program_filter', $data);
        }
        // ------
        $teacher = $this->getUser()->getTeacherObject();

        $this->forward404Unless($teacher->getIsTrainer());
        $trainer = TrainerTable::getInstance()->retrieveByIdentity($teacher->getIntgIdentity());

        $this->programs = $trainer->getPrograms();
    }

    public function executeListTeachers(sfWebRequest $request) {
        $program_id = $this->getRequestParameter('id');
        $program_obj = ProgramTable::getInstance()->find($program_id);
        $link_q = LinkTeacherProgramTable::getInstance()->getQueryByStatus(LinkTeacherProgramTable::STATUS_APPROVED);
        $link_q->leftJoin('ltp.Teacher t');
        $link_q->addWhere('ltp.program_id = ?', $program_id);
        $link_q->leftJoin('t.Attendance a');
        $link_q->leftJoin('t.School s');
        $this->teachers = $link_q->fetchArray();
        $this->setLayout('popupLayout');
    }

    public function executeExportAttendance(sfWebRequest $request) { //export
        $program_id = $this->getRequestParameter('id');
        $program_obj = ProgramTable::getInstance()->find($program_id);
        $this->program_name = $program_obj->getName();
        $fname = $program_obj->getName() . ' - ' . I18nUtilities::__('Attendance') . '.doc';
        $link_q = LinkTeacherProgramTable::getInstance()->getQueryByStatus(LinkTeacherProgramTable::STATUS_APPROVED);
        $link_q->leftJoin('ltp.Teacher t');
        $link_q->addWhere('ltp.program_id = ?', $program_id);
        $this->teachers = $link_q->fetchArray();
        $this->days = $program_obj->getPeriod();
        $this->days = array_chunk($this->days, 5);
        $html = $this->getPartial('attendance');
        header('Content-type: application/doc');
        header('Content-Disposition: attachment; filename="' . $fname . '"');
        header('Content-Description: File Transfer');
//        header("Content-Length: " . strlen($html));
        echo globals::wordify($html);
        exit;
    }

    public function executeFillAttendance(sfWebRequest $request) {

        $program_id = $this->program_id = $this->getRequestParameter('id');
        $program_obj = ProgramTable::getInstance()->find($program_id);
        $this->program_name = $program_obj->getName();
        if ($request->isMethod('POST')) {
            $data = $request->getParameter('attendance');
            AttendanceTable::getInstance()->createQuery()->delete()->where('program_id = ?', $program_id)->execute();
//            echo '<pre>';
//            print_r($data);
//            exit;
            if (count($data) > 0)
                foreach ($data as $k => $one) {
                    $pid = explode('x', $k);
                    $obj = new Attendance();
                    $obj->setProgramId($program_id);
                    $obj->setTeacherId($pid[1]);
                    $obj->setDate($pid[0]);
                    $obj->setClassShift($pid[2]);
                    try {
                        $obj->save();
                    } catch (Exception $e) {
                        
                    }
                }
            sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18n'));
            $msg = __('Attendance Saved Successfully');
            return $this->renderText("<script>alert('$msg');window.close();</script>");
        }

        $program_obj = ProgramTable::getInstance()->find($program_id);
        $fname = $program_obj->getName() . ' - ' . I18nUtilities::__('Attendance') . '.doc';
        $link_q = LinkTeacherProgramTable::getInstance()->getQueryByStatus(LinkTeacherProgramTable::STATUS_APPROVED);
        $link_q->leftJoin('ltp.Teacher t');
        $link_q->addWhere('ltp.program_id = ?', $program_id);
        $link_q->leftJoin('t.Attendance a');
        $this->teachers = $link_q->fetchArray();
        $this->days = $program_obj->getPeriod();
        $this->absences = AttendanceTable::getInstance()->createQuery()->where('program_id = ?', $program_id)->fetchArray();
        $this->setLayout('popupLayout');
    }

}
